package com.qunar.tech.team12.hotel;

import com.google.common.base.Charsets;
import com.google.common.base.Predicate;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableMultimap;
import com.google.common.collect.Iterators;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import com.google.common.io.Files;
import com.google.common.io.LineProcessor;
import com.qunar.tech.team12.hotel.bean.Hotel;
import com.qunar.tech.team12.hotel.parser.Query;

import java.io.File;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.ConcurrentMap;

public class HotelCenter {

    public static final Comparator<Hotel> PRICE = new Comparator<Hotel>() {
        @Override
        public int compare(Hotel o1, Hotel o2) {
            return o1.getPrice() - o2.getPrice();
        }
    };
    public static final Comparator<Hotel> STAR = new Comparator<Hotel>() {
        @Override
        public int compare(Hotel o1, Hotel o2) {
            return o1.getProperties().get("star").compareTo(o2.getProperties().get("star"));
        }
    };

    private static ConcurrentMap<String,List<Hotel>> hotelCache = Maps.newConcurrentMap();

    static {
        loadHotels();
    }

    public static List<Hotel> getHotels(String ciity) {
        return Lists.newArrayList();
    }

    public static void loadHotels() {
        String fileName = HotelCenter.class.getResource("/").getFile() + File.separator + "hotel.txt";
        final Splitter COMMA = Splitter.on(",");
        final Splitter EQ = Splitter.on("=");
        try {
            Files.readLines(new File(fileName), Charsets.UTF_8,new LineProcessor<String>() {
                @Override
                public boolean processLine(String line) throws IOException {
                    String[] props = line.split(",");
                    Hotel hotel = new Hotel();
                    for (String p : props) {
                        String[] kv = p.split("=");
                        //Query.ParamType.valueOf(kv[0]).setProperty(kv[1],hotel);
                    }
                    Iterator<String> property = COMMA.split(line).iterator();
                    while (property.hasNext()) {
                        Iterator<String> kv = EQ.split(property.next()).iterator();
                        String k = kv.next();
                        String v = kv.next();
                    }
                    return false;
                }

                @Override
                public String getResult() {
                    return null;
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static List<Hotel> queryHotels(final Query query) {
        Iterator<Hotel> iterator = Iterators.filter(HotelCenter.getHotels(query.getCity()).iterator(), new Predicate<Hotel>() {
            @Override
            public boolean apply(Hotel hotel) {
                return query.equals(hotel);
            }
        });
        List<Hotel> result = Lists.newArrayList(iterator);
        sortHotels(query, result);
        return Lists.newArrayList(iterator);
    }

    private static void sortHotels(Query query, List<Hotel> result) {
        if (query.getSort().equals("star")) {
            Collections.sort(result, STAR);
        } else {
            Collections.sort(result, PRICE);
        }
        if (query.getOrder().equals("desc")) {
            Collections.reverse(result);
        }
    }
}